#include "ExtractingIndicesModel.h"

#include "core/AppDirectory.h"

namespace {
const QString UserFilename = "pcl.extracting_indices_model.json";
}

ExtractingIndicesModel::ExtractingIndicesModel()
    : BasePclModel()
{
    setPclHelpPage("https://pcl.readthedocs.io/projects/tutorials/en/master/extract_indices.html");
}

ExtractingIndicesModel& ExtractingIndicesModel::get()
{
    static ExtractingIndicesModel instance;
    if (instance.filename().isEmpty()) {
        instance.setFilename(AppDirectory::get().getAppDocFile(UserFilename));
        instance.load();
    }
    return instance;
}

void ExtractingIndicesModel::fromJson(const QJsonObject& json, QStringList& errorList)
{
    BasePclModel::fromJson(json, errorList);

    pcdFile_ = json["pcdFile"].toString(pcdFile_);
    leafSizeForX_ = json["leafSizeForX"].toDouble(leafSizeForX_);
    leafSizeForY_ = json["leafSizeForY"].toDouble(leafSizeForY_);
    leafSizeForZ_ = json["leafSizeForZ"].toDouble(leafSizeForZ_);
    maxIterations_ = json["maxIterations"].toInt(maxIterations_);
    distanceThreshold_ = json["distanceThreshold"].toDouble(distanceThreshold_);
    keepRatio_ = json["keepRatio"].toDouble(keepRatio_);
}

QJsonObject ExtractingIndicesModel::toJson() const
{
    QJsonObject json = BasePclModel::toJson();

    json.insert("pcdFile", QJsonValue(pcdFile_));
    json.insert("leafSizeForX", QJsonValue(leafSizeForX_));
    json.insert("leafSizeForY", QJsonValue(leafSizeForY_));
    json.insert("leafSizeForZ", QJsonValue(leafSizeForZ_));
    json.insert("maxIterations", QJsonValue(maxIterations_));
    json.insert("distanceThreshold", QJsonValue(distanceThreshold_));
    json.insert("keepRatio", QJsonValue(keepRatio_));

    return json;
}
